﻿using AutoMapper;
using MediatR;
using Medical.Api.Write.Application.Command.RecyclingCleaning.Disinfects;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Medical.Interstructrue;
using Medical.Interstructrue.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Medical.Api.Write.Application.Handler.RecyclingCleaning.Disinfects
{
    /// <summary>
    /// 添加消毒登记信息命令处理
    /// </summary>
    public class AddDisinfectionCommandHandlter : IRequestHandler<AddDisinfectionCommand, ApiResult<int>>
    {
        /// <summary>
        /// 消毒登记仓储
        /// </summary>
        private readonly IBaseRepository<DisinfectionRegistration> _disinfectionRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="disinfectionRepository">消毒登记仓储</param>

        public AddDisinfectionCommandHandlter(IBaseRepository<DisinfectionRegistration> disinfectionRepository, IMapper mapper)
        {
            _disinfectionRepository = disinfectionRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(AddDisinfectionCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 生成消毒批号，格式为 XD45001 + 随机数
                string batchNumber = GenerateBatchNumber();

                // 创建消毒登记信息
                var disinfectionRegistration = mapper.Map<DisinfectionRegistration>(request);
                disinfectionRegistration.BatchNumber = batchNumber;
                disinfectionRegistration.EquipmentId = request.EquipmentId;
                disinfectionRegistration.Operator = request.Operator;
                disinfectionRegistration.StartTime = request.StartTime;
                disinfectionRegistration.DisinfectionMode = request.DisinfectionMode;
                disinfectionRegistration.DisinfectionTemperature = request.DisinfectionTemperature;
                disinfectionRegistration.Status = 0;
                disinfectionRegistration.ItemIds = request.ItemIds;
                disinfectionRegistration.CreateTime = DateTime.Now;
                disinfectionRegistration.UpdateTime = DateTime.Now;
                disinfectionRegistration.CompletionOperator= request.Operator;
                disinfectionRegistration.DisinfectionDuration = 0;
                disinfectionRegistration.DryingDuration = 0;
                disinfectionRegistration.CompletionTime = DateTime.Now;
                disinfectionRegistration.DryingTemperature = 0;
                disinfectionRegistration.CreateUserId = 0;
                disinfectionRegistration.UpdateUserId = 0;
                disinfectionRegistration.CreateUserName = "";
                disinfectionRegistration.UpdateUserName = "";
                disinfectionRegistration.DisinfectionEffect= "";
                disinfectionRegistration.DryingMode = "";
                disinfectionRegistration.DryingOperator = "";
         

                // 保存到数据库
                int result = _disinfectionRepository.Create(disinfectionRegistration);

                if (result > 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Msg = "创建消毒登记成功",
                        Data = result

                    };
                }

                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Msg = "创建消毒登记失败",
                    Data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Msg = $"创建消毒登记失败{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 生成消毒批号，格式为 XD45001 + 随机数
        /// </summary>
        private string GenerateBatchNumber()
        {
            string prefix = "XD45001";
            Random random = new Random();
            int randomNumber = random.Next(10000, 100000); // 5位随机数
            return $"{prefix}{randomNumber}";
        }
    }
}
